package com.lixiaoyi.ems.controller;

import com.lixiaoyi.ems.entity.Emp;
import com.lixiaoyi.ems.entity.Page;
import com.lixiaoyi.ems.service.IEmpService;
import com.lixiaoyi.ems.service.impl.EmpServiceImpl;
import com.sun.net.httpserver.HttpServer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author 李小毅
 * @Description
 * @Date 2022-05-18
 */

@WebServlet("/manager/safe/showAllEmp")
public class ShowAllEmpController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.对request中的pageNum判断
        String pageNum = request.getParameter("pageNum");
        //如果为空  赋值为第一页
        if (pageNum == null){
            pageNum = "1";
        }
        //确定Page参数  以便分页
        Page page = new Page(Integer.valueOf(pageNum));

        System.out.println(page.getPageNum());
        System.out.println(page.getTotalPages());
        //调用service层分页方法
        IEmpService empService = new EmpServiceImpl();
        List<Emp> emps = empService.showAllEmpByPage(page);
        //传入作用域
        request.setAttribute("emps",emps);
        request.setAttribute("page",page);
        //转发 emplist页面 展示所有
        request.getRequestDispatcher("/emplist.jsp").forward(request,response);


    }
}
